package com.itheima.reggin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggin.common.BaseContext;
import com.itheima.reggin.common.R;
import com.itheima.reggin.entity.ShoppingCart;
import com.itheima.reggin.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *判断当前的菜品或者套餐在不在购物车里面，如果在就把数量+1;不在就把信息插入到购物车，数量设置为1
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){


        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper<>();
        query.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            //添加到购物车的是菜品
            query.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            query.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(query);

        if(cartServiceOne != null){
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    /**
     * 查
     * 主要首页用户登录时显示购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 删除全部
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");

    }
    /**
     * 删除单个
     */
    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){

        Long currentId = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper<>();
        query.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();
        //删除类型菜品套餐
        if(dishId==null){
            query.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            query.eq(ShoppingCart::getDishId,dishId);
        }

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(query);
        int number = shoppingCartOne.getNumber();
        if(number>1){
            shoppingCartOne.setNumber(--number);
            shoppingCartService.updateById(shoppingCartOne);
        }else if(number==1){
            shoppingCartService.remove(query);
        }
        return R.success("删除成功！");
    }
}